package fr.oz.mobile.module.profile.nav 
{
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import fr.digitas.flowearth.text.styles.styleManager;
	import fr.oz.core.datas.ProfilDatas;
	import fr.oz.core.datas.ProfilList;
	import fr.oz.core.datas.ProfilListDatas;
	import fr.oz.core.events.ProfilEvent;
	import fr.oz.core.utils.Utils;
	import fr.oz.mobile.module.profile.ProfileDeleteCross_FC;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class ProfilNavItem extends Sprite
	{
		
		private var _profil			: ProfilListDatas;
		private var deleteCross		: ProfileDeleteCross_FC;
		private var tf_hour			: TextField;
		private var tf_minut		: TextField;
		private var nameManager		: ProfilNavItemNameManager;
		private var weekManager		: ProfilNavItemWeekManager;
		private var activator		: ProfilNavItemActivator;
		
		public function ProfilNavItem(profil:ProfilListDatas) 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			
			_profil = new ProfilListDatas(profil._node);
			
			deleteCross = new ProfileDeleteCross_FC();
			deleteCross.x = 45;
			deleteCross.y = 42;
			addChild(deleteCross);
			
			tf_hour = new TextField();
			tf_minut = new TextField();
			
			styleManager.apply(tf_hour, ".profile_module_pad_profil_hour", Utils.addZero(profil.time_hour));
			styleManager.apply(tf_minut, ".profile_module_pad_profil_minut", Utils.addZero(profil.time_minut));
			
			addChild(tf_hour);
			addChild(tf_minut);
			tf_hour.x = 85;
			tf_minut.x = 85 + tf_hour.width - 3;
			
			nameManager= new ProfilNavItemNameManager(profil.name);
			nameManager.x = 185;
			nameManager.y = tf_hour.y + tf_hour.height - nameManager.height - 2;
			addChild(nameManager);
			
			weekManager = new ProfilNavItemWeekManager(profil.dayInWeek);
			addChild(weekManager);
			weekManager.x = 85;
			weekManager.y = tf_hour.y + tf_hour.height - 5;
			
			if (!profil.activated) {
				tf_hour.alpha = tf_minut.alpha = nameManager.alpha = .5;
			}
			
			activator = new ProfilNavItemActivator(profil.activated);
			activator.x = 375;
			activator.y = 23;
			addChild(activator);
			activator.addEventListener(Event.CHANGE, handleChangeActiveProfil);
			
		}
		
		private function handleChangeActiveProfil(e:Event):void 
		{
			if (profil.activated != activator.isActivate())
			{
				profil.activated = activator.isActivate();
				if (!profil.activated) {
					Tweener.addTween([tf_hour, tf_minut, nameManager], { alpha:.5, time:.5 } );
				}else {
					Tweener.addTween([tf_hour, tf_minut, nameManager], { alpha:1, time:.5 } );
				}
				//dispatchEvent(new ProfilEvent(ProfilEvent.PROFIL_ACTIV_SWITCH, profil, true));
				ProfilList.UPDATE(profil.ID, profil);
			}
		}
		
		public function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			deleteCross.cross.alpha = 0;
			deleteCross.cross.rotation = 375;
			Tweener.addTween(deleteCross.cross, { rotation:0, alpha:1, delay:.5, time:1 } );
			deleteCross.bg.scaleX = deleteCross.bg.scaleY = .5;
			deleteCross.bg.alpha = 0;
			Tweener.addTween(deleteCross.bg, {scaleX:1, scaleY:1, alpha:1,delay:.7, time:.5, transition:"easeOutBack" } );
			
			this.graphics.beginFill(0xffffff, 0);
			this.graphics.drawRect(0, 0, stage.stageWidth, 85);
			this.graphics.endFill();
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
		}
		
		public function get profil():ProfilListDatas { return _profil; }
		
		public function select():void
		{
			Tweener.addTween(deleteCross.cross, { rotation:375, alpha:0, time:1 } );
			Tweener.addTween(deleteCross.bg, {scaleX:.5, scaleY:.5, alpha:0, time:.5, transition:"easeInBack" } );
		}
		
	}

}